import {_decorator, Component, log, macro, Mat4, mat4, Node, quat, Quat, systemEvent, SystemEvent, v3, Vec3} from 'cc';
import {Player} from "./Player";
import {NodeSpace} from "../../../engine_1.2.0/cocos/core/scene-graph/node-enum";

const { ccclass, property } = _decorator;

const v3_1 = v3();
const quat_1 = quat();
Quat.fromEuler(quat_1,0,2,0);

const quat_2 = quat();
Quat.fromEuler(quat_2,0,-2,0);

const quat_x = quat();
Quat.fromEuler(quat_x,2,0,0);

const quat_nx = quat();
Quat.invert(quat_nx,quat_x);

// const quat_tnx = quat();
// Quat.fromEuler(quat_tnx,-2,0,0);
//
// log('quat_nx:',quat_nx);
// log('quat_tnx:',quat_tnx);

@ccclass('GameCtrl')
export class GameCtrl extends Component {
    /* class member could be defined like this */
    // dummy = '';

    /* use `property` decorator if your want the member to be serializable */
    // @property
    // serializableDummy = 0;

    @property({type:Player})
    player:Player = null;

    onLoad(){
        systemEvent.on(SystemEvent.EventType.KEY_DOWN,this.onKeyDown,this);
    }



    start () {
        // Your initialization goes here.
    }

    onKeyDown(e){

        // ws 前进/后退
        // ad 左右 移动
        // j 跳跃
        // q/e 左转/右转
        // up/down 抬头/低头
        let forward = this.player.node.forward;
        // log('forward_before:',forward);
        // Vec3.normalize(forward,forward);
        // log('forward_after:',forward);
        switch (e.keyCode) {
            case macro.KEY.w:
                // 朝forward方向前进
                // this.player.node.translate(Vec3.multiplyScalar(v3_1,forward,2));

                // 不改变y
                Vec3.multiplyScalar(v3_1,forward,2);
                v3_1.y = 0;
                Vec3.add(v3_1,this.player.node.position,v3_1);
                this.player.node.setPosition(v3_1);
                break;
            case macro.KEY.s:
                // this.player.node.translate(Vec3.multiplyScalar(v3_1,forward,-2));
                Vec3.multiplyScalar(v3_1,forward,-2);
                v3_1.y = 0;
                Vec3.add(v3_1,this.player.node.position,v3_1);
                this.player.node.setPosition(v3_1);
                break;
            case macro.KEY.j:
                this.player.jump();
                break;
            case macro.KEY.a:
                // 左移
                // 先求left
                Vec3.cross(v3_1,Vec3.UNIT_Y,this.player.node.forward);
                Vec3.normalize(v3_1,v3_1);
                Vec3.multiplyScalar(v3_1,v3_1,2);
                Vec3.add(v3_1,this.player.node.position,v3_1);
                this.player.node.setPosition(v3_1);
                break;
            case macro.KEY.d:
                Vec3.cross(v3_1,Vec3.UNIT_Y,this.player.node.forward);
                Vec3.normalize(v3_1,v3_1);
                Vec3.multiplyScalar(v3_1,v3_1,-2);
                Vec3.add(v3_1,this.player.node.position,v3_1);
                this.player.node.setPosition(v3_1);
                break;
            case macro.KEY.q:
                // 左转
                this.player.node.rotate(quat_1,Node.NodeSpace.WORLD);
                break;
            case macro.KEY.e:
                // 右转
                this.player.node.rotate(quat_2,Node.NodeSpace.WORLD);
                break;
            case macro.KEY.up:
                this.player.node.rotate(quat_x,Node.NodeSpace.LOCAL);
                break;
            case macro.KEY.down:
                this.player.node.rotate(quat_nx,Node.NodeSpace.LOCAL);
                break;
            default:
                break;
        }
    }
}
